From 9671c682011cf0635ae0effb40abc4633fbed4de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 2 Jul 2017 11:01:25 +0300 Subject: [PATCH] Make `Layout` a private implementation detail of toml --- src/cargo/core/workspace.rs | 7 +++--- src/cargo/ops/cargo_read_manifest.rs | 17 ++----------- src/cargo/ops/mod.rs | 2 +- src/cargo/util/toml.rs | 36 ++++++++++++++++++---------- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 36df965eb..a953f72c4 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -7,10 +7,10 @@ use glob::glob; use core::{Package, VirtualManifest, EitherManifest, SourceId}; use core::{PackageIdSpec, Dependency, Profile, Profiles}; -use ops; use util::{Config, Filesystem}; use util::errors::{CargoResult, CargoResultExt}; use util::paths; +use util::toml::read_manifest; /// The core abstraction in Cargo for working with a workspace of crates. /// @@ -594,9 +594,8 @@ impl<'cfg> Packages<'cfg> { Entry::Occupied(e) => Ok(e.into_mut()), Entry::Vacant(v) => { let source_id = SourceId::for_path(key)?; - let pair = ops::read_manifest(&manifest_path, &source_id, - self.config)?; - let (manifest, _nested_paths) = pair; + let (manifest, _nested_paths) = + read_manifest(&manifest_path, &source_id, self.config)?; Ok(v.insert(match manifest { EitherManifest::Real(manifest) => { MaybePackage::Package(Package::new(manifest, diff --git a/src/cargo/ops/cargo_read_manifest.rs b/src/cargo/ops/cargo_read_manifest.rs index 98cfde1d3..0f93b3bfd 100644 --- a/src/cargo/ops/cargo_read_manifest.rs +++ b/src/cargo/ops/cargo_read_manifest.rs @@ -4,23 +4,10 @@ use std::io; use std::path::{Path, PathBuf}; use core::{Package, SourceId, PackageId, EitherManifest}; -use util::{self, paths, Config}; +use util::{self, Config}; use util::errors::{CargoResult, CargoResultExt}; use util::important_paths::find_project_manifest_exact; -use util::toml::Layout; - -pub fn read_manifest(path: &Path, source_id: &SourceId, config: &Config) - -> CargoResult<(EitherManifest, Vec)> { - trace!("read_package; path={}; source-id={}", path.display(), source_id); - let contents = paths::read(path)?; - - let layout = Layout::from_project_path(path.parent().unwrap()); - let root = layout.root.clone(); - util::toml::to_manifest(&contents, source_id, layout, config).chain_err(|| { - format!("failed to parse manifest at `{}`", - root.join("Cargo.toml").display()) - }) -} +use util::toml::read_manifest; pub fn read_package(path: &Path, source_id: &SourceId, config: &Config) -> CargoResult<(Package, Vec)> { diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 8d8908320..8440468ef 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -1,7 +1,7 @@ pub use self::cargo_clean::{clean, CleanOptions}; pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOptions}; pub use self::cargo_compile::{CompileFilter, CompileMode, MessageFormat, Packages}; -pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages}; +pub use self::cargo_read_manifest::{read_package, read_packages}; pub use self::cargo_rustc::{compile_targets, Compilation, Kind, Unit}; pub use self::cargo_rustc::{Context, is_bad_artifact_name}; pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig}; diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index bba874791..cb684fa02 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -18,16 +18,13 @@ use core::dependency::{Kind, Platform}; use core::manifest::{LibKind, Profile, ManifestMetadata}; use ops::is_bad_artifact_name; use sources::CRATES_IO; +use util::paths; use util::{self, ToUrl, Config}; use util::errors::{CargoError, CargoResult, CargoResultExt}; -/// Representation of the projects file layout. -/// -/// This structure is used to hold references to all project files that are relevant to cargo. - -#[derive(Clone)] -pub struct Layout { - pub root: PathBuf, +/// Implicit Cargo targets, defined by conventions. +struct Layout { + root: PathBuf, lib: Option, bins: Vec, examples: Vec, @@ -38,7 +35,7 @@ pub struct Layout { impl Layout { /// Returns a new `Layout` for a given root path. /// The `root_path` represents the directory that contains the `Cargo.toml` file. - pub fn from_project_path(root_path: &Path) -> Layout { + fn from_project_path(root_path: &Path) -> Layout { let mut lib = None; let mut bins = vec![]; let mut examples = vec![]; @@ -114,11 +111,24 @@ fn try_add_files(files: &mut Vec, root: PathBuf) { /* else just don't add anything if the directory doesn't exist, etc. */ } -pub fn to_manifest(contents: &str, - source_id: &SourceId, - layout: Layout, - config: &Config) - -> CargoResult<(EitherManifest, Vec)> { +pub fn read_manifest(path: &Path, source_id: &SourceId, config: &Config) + -> CargoResult<(EitherManifest, Vec)> { + trace!("read_manifest; path={}; source-id={}", path.display(), source_id); + let contents = paths::read(path)?; + + let layout = Layout::from_project_path(path.parent().unwrap()); + let root = layout.root.clone(); + to_manifest(&contents, source_id, layout, config).chain_err(|| { + format!("failed to parse manifest at `{}`", + root.join("Cargo.toml").display()) + }) +} + +fn to_manifest(contents: &str, + source_id: &SourceId, + layout: Layout, + config: &Config) + -> CargoResult<(EitherManifest, Vec)> { let manifest = layout.root.join("Cargo.toml"); let manifest = match util::without_prefix(&manifest, config.cwd()) { Some(path) => path.to_path_buf(), -- 2.30.2